package com.kiln.juxingqs.ui.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import client.global.commlib.network.data.JsonResult
import client.global.commlib.network.data.Ret
import client.global.commlib.toast.toast
import client.global.commlib.utils.StoreUtil
import com.kiln.juxingqs.api.bean.GoodsDetail
import com.kiln.juxingqs.api.bean.UserInfo
import com.kiln.juxingqs.ui.viewmodel.MiddleVm

class SyDetailModel(app: Application) : MiddleVm(app) {


    val goodsDetail = MutableLiveData<JsonResult<GoodsDetail>>(null)

    val priceBuy = MutableLiveData<String>("")
    val isShowing = MutableLiveData<Boolean>(false)
    val isRefing = MutableLiveData<Boolean>(false)
    val buyNum = MutableLiveData<String>("")

    val priceSell = MutableLiveData<String>("")
    val sellNum = MutableLiveData<String>("")
    val goodId = MutableLiveData<String>("")
    val guanzRet = MutableLiveData<JsonResult<Ret>>()
    val guanSellRet = MutableLiveData<JsonResult<Ret>>()
    val guanBuyRet = MutableLiveData<JsonResult<Ret>>()
    val callUser = MutableLiveData<JsonResult<UserInfo>>()

    val zhRet = MutableLiveData<JsonResult<Ret>>()

    fun holdCall(id: String) {
        if (isRefing.value!!) {
            return
        }
        hold({
            server.goods_detail("${id}", "")
        }, {
            isRefing.value = true
        }, success = {
            goodsDetail.value = it
        }, complete = {
            isRefing.value = false
        })
    }

    fun zhuanhuanPost(id: String, num: String) {
        hold({
            server.freeze_locking_transfer(id, num)
        }, {
            isShowing.value = true
        }, success = {
            zhRet.value = it
        }, complete = {
            isShowing.value = false
        })
    }

    fun add_gz() {
        hold({
            server.gz_order(
                "${goodsDetail.value!!.data!!.goods_id!!}"
            )
        }, {
            isShowing.value = true
        }, success = {
            guanzRet.value = it
        }, complete = {
            isShowing.value = false
        })
    }

    fun guaMai() {
        hold({
            server.buyer_order(
                "${goodsDetail.value!!.data!!.goods_id!!}",
                "${buyNum.value!!}",
                "${priceBuy.value!!}",
            )
        }, {
            isShowing.value = true
        }, success = {
            guanBuyRet.value = it
        }, complete = {
            isShowing.value = false
        })
    }

    fun guaiSell() {
        hold({
            server.sell_order2(
                "${goodId.value!!}",
                "${sellNum.value!!}",
                "${priceSell.value!!}",
            )
        }, {
            isShowing.value = true
        }, success = {
            guanSellRet.value = it
        }, complete = {
            isShowing.value = false
        })
    }

//    fun guaiSell(b: GetBathRet.Bath) {
//        if (priceSell.value.isNullOrEmpty()) {
//            toast("请输入挂卖价格")
//            return
//        }
//        if (sellNum.value.isNullOrEmpty()) {
//            toast("请输入挂卖数")
//            return
//        }
//        if (priceSell.value!!.toDouble() < goodsDetail.value!!.data!!.sell_start
//            || priceSell.value!!.toDouble() > goodsDetail.value!!.data!!.sell_end
//        ) {
//            toast("价格不能超出范围")
//            return
//        }
//        hold({
//            server.sell_order(
//                "${b.bath_id}",
//                "${sellNum.value!!}",
//                "${priceSell.value!!}",
//            )
//        }, {
//            isShowing.value = true
//        }, success = {
//            guanSellRet.value = it
//        }, complete = {
//            isShowing.value = false
//        })
//    }

    fun userInfo() {
        hold({
            server.userinfo().apply {
                body()?.let {
                    if (it.isSuccess) {
                        StoreUtil.saveBean(getApplication(), it.data!!)
                    }
                }
            }
        }, success = {
            callUser.value = it
        })
    }
}